<?php


namespace app\controller\goods;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 单位
 */
class Units extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取单位列表
     */
	public function api_goods_units_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:''
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['name'])){
            $where .= " AND name LIKE '%".$info['name']."%'";
        }
		$list = $db->query("select * from goods_units where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from goods_units WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}



    /**
     * 添加
     */
    public function api_goods_units_add($msg)
    {   
        $info = [
            'name' =>  isset($msg['data']['name'])?$msg['data']['name']:'',
            'intro' => isset($msg['data']['intro'])?$msg['data']['intro']:''
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        if(!$this->validate->checkName($info['intro'],2,100)){
            return $this->outMessage(-1,'请输入正确的简介');
        }
        $res = $this->table->find('goods_units',['name'=>$info['name']]);
        if($res){
            return $this->outMessage(-1,'该单位已存在');
        }
        $data = [
            'name' => $info['name'],
            'intro' => $info['intro'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('goods_units',$data);
        if(!$res){
            return $this->outMessage(-1,'添加失败');
        }
        return $this->outMessage(10000,'添加成功');
    }



    /**
     * 编辑
     */
    public function api_goods_units_edit($msg)
    {   
        $info = [
            'id' =>  isset($msg['data']['id'])?$msg['data']['id']:'',
            'intro' => isset($msg['data']['intro'])?$msg['data']['intro']:''
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        if(!$this->validate->checkName($info['intro'],2,100)){
            return $this->outMessage(-1,'请输入正确的简介');
        }
        $data = [
            'intro' => $info['intro'],
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('goods_units',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
    }



    /**
     * 删除
     */
    public function api_goods_units_delete($msg)
    {
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(empty($id)){
            return $this->outMessage(-1,'ID不能为空');
        }
        $res = $this->table->delete('goods_units',['id'=>$id]);
        if(!$res){
            return $this->outMessage(-1,'删除失败');
        }
        return $this->outMessage(10000,'删除成功');
    }
 

}
